package com.acme.domain.client;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.acme.domain.account.Account;

public class Client implements Serializable {

	private String name;
	private Gender gender;

	private List<Account> accounts = new ArrayList<Account>();

	public Client(final String name, final Gender gender) {
		this.name = name;
		this.gender = gender;
	}

	public String getName() {
		return name;
	}

	public String getSalutation() {
		if (gender != null) {
			return gender.getSalutation() + " " + name;
		} else {
			return name;
		}
	}

	public Account getAccount(final int id) {
		for (Account acc : accounts) {
			if (acc.getAccountNumber() == id) {
				return acc;
			}
		}
		return null;
	}

	public void addAccount(final Account account) {
		accounts.add(account);
	}

	public String toString() {
		StringBuffer tStr = new StringBuffer("Client information: ");
		tStr.append(getSalutation());
		for (Account clientAcc : accounts) {
			tStr.append(clientAcc.toString());
		}
		String clientInfo = tStr.toString();

		return clientInfo;

	}

	public List<Account> getAccountsList() {
		return Collections.unmodifiableList(accounts);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 31;
		if (accounts != null) {
			result += accounts.hashCode();
		}
		result *= prime;
		if (gender != null) {
			result += gender.hashCode();
		}
		result *= prime;
		if (name != null) {
			result += gender.hashCode();
		}
		return result;
	}

	public boolean equals(Object obj) {
		Client other;
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (obj instanceof Client) {
			other = (Client) obj;
		} else {
			return false;
		}
		if (this.name != other.name) {
			return false;
		}
		if (this.gender != other.gender) {
			return false;
		}
		if (!(this.accounts.equals(other.accounts))) {
			return false;
		}
		return true;
	}

}
